﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using AmazonGlacierGUI.Core.Infrastructure;
using AmazonGlacierGUI.Core.Model;
using Raven.Client.Embedded;

namespace AmazonGlacierGUI.Core
{
    public class AppContext
    {
        public void Init()
        {
            try
            {

                StartDB();
                LoadApSettings();
                
            }
            catch (Exception ex)
            {
                throw new Exception("Cannot start Database " + ex.Message);                
            }
        }
        public void LoadApSettings()
        {
            Settings = GetServiceFactory().AppService.GetAppSettings();
            
        }        
        void StartDB()
        {                        
            _documentStore = new EmbeddableDocumentStore
            {
                DataDirectory = "Data"           
            };
            var s = ConfigurationManager.AppSettings["Raven.UseHttpServer"];
            if (s != null && bool.Parse(s))
            {
                _documentStore.UseEmbeddedHttpServer = true;
                _documentStore.Configuration.Port =int.Parse(ConfigurationManager.AppSettings["Raven.HttpServerPort"]);
            }
            _documentStore.Initialize();
        }
        
        public AppSettings Settings;
        public ServiceFactory GetServiceFactory()
        {
            return new ServiceFactory(_documentStore.OpenSession());
        }
        private EmbeddableDocumentStore _documentStore;
        public static AppContext Current { get; set; }
    }

}
